package com.activity.shop;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.activity.App;
import com.activity.AttendanceActivity;
import com.activity.adapter.ApplicationAdapter;
import com.activity.adapter.TimeChooseAdapter;
import com.activity.bean.ApplicationBean;
import com.activity.bean.ChooseTimeBean;
import com.activity.bean.ClassSpan;
import com.activity.bean.TimeBean;
import com.activity.bean.UserBean;
import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;
import com.donkingliang.groupedadapter.layoutmanger.GroupedGridLayoutManager;
import com.example.apple.ddhr_new.R;
import com.example.apple.ddhr_new.activity.setting.BaseListViewActivity;
import com.example.apple.ddhr_new.utils.ActivityUtil;
import com.example.apple.ddhr_new.utils.ToastUtil;
import com.example.lzbase.BaseViewModelActivity;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class TimeChooseActivity extends BaseListViewActivity {


    private TimeChooseAdapter timeChooseAdapter;
    private int selectIndex;

    @Override
    protected int titleText() {
        return R.string.kaoqin_riqi;
    }

    @Override
    protected void created(@Nullable Bundle savedInstanceState) {
        super.created(savedInstanceState);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setEnableRefresh(false);
        timeChooseAdapter = new TimeChooseAdapter(this);

        TimeBean day1 = new TimeBean("每周一","请选择班次");
        TimeBean day2 = new TimeBean("每周二","请选择班次");
        TimeBean day3 = new TimeBean("每周三","请选择班次");
        TimeBean day4 = new TimeBean("每周四","请选择班次");
        TimeBean day5 = new TimeBean("每周五","请选择班次");
        TimeBean day6 = new TimeBean("每周六","休息");
        TimeBean day7 = new TimeBean("每周日","休息");
        timeChooseAdapter.getData().add(day1);
        timeChooseAdapter.getData().add(day2);
        timeChooseAdapter.getData().add(day3);
        timeChooseAdapter.getData().add(day4);
        timeChooseAdapter.getData().add(day5);
        timeChooseAdapter.getData().add(day6);
        timeChooseAdapter.getData().add(day7);
        String param =  getIntent().getStringExtra("param");
        String span =  getIntent().getStringExtra("span");
        Gson gson =new Gson();
        if(param==null){
            if(span!=null)
            {


                for (int i=0;i<7;i++){
                    TimeBean timeBean = timeChooseAdapter.getData().get(i);
                    if(timeBean.getClassSpan()==null){
                        timeBean.setClassSpan(gson.fromJson(span,ClassSpan.class));
                    }

                }
            }
        }else {

            ChooseTimeBean bean = gson.fromJson(param,ChooseTimeBean.class);

            for (int i=0;i<timeChooseAdapter.getData().size();i++){
                for (int j=0;j<bean.timeBeans.size();j++){

                    if(timeChooseAdapter.getData().get(i).getName().equals(bean.timeBeans.get(j).getName())){
                        timeChooseAdapter.getData().get(i).setClassSpan(bean.timeBeans.get(j).getClassSpan());
                        timeChooseAdapter.getData().get(i).selected = true;
                        break;
                    }
                }
            }
            if(span!=null)
            {


                for (int i=0;i<7;i++){
                    TimeBean timeBean = timeChooseAdapter.getData().get(i);
                    if(timeBean.getClassSpan()==null){
                        timeBean.setClassSpan(gson.fromJson(span,ClassSpan.class));
                    }

                }
            }
        }
        recycleView.setAdapter(timeChooseAdapter);
        timeChooseAdapter.timeListener = new TimeChooseAdapter.TimeListener() {
            @Override
            public void editClick(int ChildPosition) {

                selectIndex = ChildPosition;
                ActivityUtil.startActivity(TimeChooseActivity.this,ClassActivity.class,new Bundle(),1000);
            }

            @Override
            public void doneClick() {
                ArrayList<TimeBean> spans = new ArrayList<>();
                for (int i=0;i<timeChooseAdapter.getData().size();i++){
                    if(timeChooseAdapter.getData().get(i).selected){
                        spans.add(timeChooseAdapter.getData().get(i));
                    }
                }
                if(spans.size()==0){
                    ToastUtil.toast(TimeChooseActivity.this,"请选择考勤日期");
                }else {

                    Intent intent =  new Intent();

                    ChooseTimeBean chooseTimeBean = new ChooseTimeBean();
                    chooseTimeBean.timeBeans = spans;
                    Gson gson =new Gson();
                    intent.putExtra("data",gson.toJson(chooseTimeBean));
                    setResult(100,intent);
                    finish();

                }
            }
        };

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode==1000&&resultCode==100){
            Gson gson =new Gson();
            ClassSpan classSpan = gson.fromJson(data.getStringExtra("data"),ClassSpan.class);

            timeChooseAdapter.getData().get(selectIndex).setClassSpan(classSpan);

            timeChooseAdapter.notifyChildChanged(0,selectIndex);
        }
    }
}
